package com.meihua.workflow.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.meihua.workflow.constants.Constants;
import com.meihua.workflow.constants.R;
import com.meihua.workflow.model.Contract;
import com.meihua.workflow.model.ContractDetail;
import com.meihua.workflow.model.vo.ContractVo;
import com.meihua.workflow.service.ContractDetailService;
import com.meihua.workflow.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController {
    @Resource
    private ContractService contractService;
    @Resource
    private ContractDetailService contractDetailService;
    @GetMapping("/contractPage")
    public R contractPage(@RequestParam(value = "pageNo",defaultValue = "1",required = false) Integer pageNo,
                          @RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize){
        IPage<Contract> contractPage = new Page<>(pageNo, pageSize);
        return R.success(this.contractService.page(contractPage));
    }
    @PostMapping("/createOrUpdateContract")
    public R createOrUpdateContract(@RequestBody  @Validated ContractVo contractVo ){
        if (BeanUtil.isEmpty(contractVo.getContractDetail())){
            return R.error("未输入合同行信息");
        }
        LambdaQueryWrapper<Contract> contractLambdaQueryWrapper = new LambdaQueryWrapper<>();
        contractLambdaQueryWrapper.eq(Contract::getContractNum,contractVo.getContractNum());
        Contract one = this.contractService.getOne(contractLambdaQueryWrapper);
        Contract contract = new Contract();
        BeanUtil.copyProperties(contractVo,contract);
        //更新
        if (BeanUtil.isNotEmpty(one)){
            this.contractService.updateById(contract);
            this.contractDetailService.remove(new LambdaQueryWrapper<ContractDetail>().eq(ContractDetail::getContractNum,contractVo.getContractNum()));
        }
        //新建
       else {
            contract.setContractNum(Constants.SC_PREFIX+ DateUtil.format(new Date(),"yyyyMMdd")+ LocalTime.now().getNano());
            this.contractService.save(contract);
        }
        for (ContractDetail c:
                contractVo.getContractDetail()) {
            c.setContractNum(contractVo.getContractNum());
        }
        System.out.println(contractVo.getContractDetail());
        this.contractDetailService.saveBatch(contractVo.getContractDetail());
        return R.success();
    }
    @GetMapping("/{contractNum}")
    public R getContract(@PathVariable("contractNum")String contractNum){
        if (StrUtil.isEmpty(contractNum)){
            return R.error("合同编号为空");
        }
        Contract one = this.contractService.getOne(new LambdaQueryWrapper<Contract>().eq(Contract::getContractNum, contractNum));
        List<ContractDetail> list = this.contractDetailService.list(new LambdaQueryWrapper<ContractDetail>().eq(ContractDetail::getContractNum, contractNum));
        ContractVo contractVo = new ContractVo();
        BeanUtil.copyProperties(one,contractVo);
        contractVo.setContractDetail(list);
        return R.success(contractVo);
    }
    @Transactional
    @DeleteMapping("/{contractNum}")
    public R deleteContract(@PathVariable("contractNum") String contractNum){
        if (StrUtil.isEmpty(contractNum)){
            return R.error("合同编号为空");
        }
        boolean removeContractDetail = this.contractDetailService.remove(new LambdaQueryWrapper<ContractDetail>().eq(ContractDetail::getContractNum, contractNum));
        boolean removeContract = this.contractService.remove(new LambdaQueryWrapper<Contract>().eq(Contract::getContractNum, contractNum));
        return removeContract&&removeContractDetail?R.success():R.error("合同删除失败");
    }
    public R aa(){

        return R.success(Constants.SC_PREFIX+ DateUtil.format(new Date(),"yyyyMMdd")+ LocalTime.now().getNano());

    }

}
